//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.eResearch.Blackboard.Wcf
{
    using System;
    using System.ServiceModel;
    using Microsoft.Research.eResearch.Blackboard.Common;
    using Microsoft.Research.eResearch.Blackboard.Interfaces;
    using Microsoft.Research.eResearch.Common.Eventing;

    /// <summary>
    /// Manages the hosting of a <see cref="WcfNotificationSubscriptionService"/>.
    /// </summary>
    public class WcfNotificationSubscriptionServiceHost : INotificationSubscriptionReceiver
    {
        /// <summary>
        /// Instance of WCF service host.
        /// </summary>
        private ServiceHost serviceHost;

        /// <summary>
        /// Initializes a new instance of the <see cref="WcfNotificationSubscriptionServiceHost"/> class.
        /// </summary>
        public WcfNotificationSubscriptionServiceHost()
        {
        }

        /// <summary>
        /// Occurs when a subscriber requests to be unsubscribed from all BlackboardMessages.
        /// </summary>
        public event EventHandler<NotificationUnsubscriptionEventArgs> NotificationUnsubscription;

        /// <summary>
        /// Occurs when a new subscriber requests to be notified of particular BlackboardMessages.
        /// </summary>
        public event EventHandler<NotificationSubscriptionEventArgs> NotificationSubscription;

        /// <summary>
        /// Begins hosting a <see cref="WcfNotificationSubscriptionService"/> to listen for subscription/unsubscription
        /// messages from subsribers.
        /// </summary>
        public void Start()
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
                serviceHost = null;
            }

            serviceHost = new ServiceHost(typeof(WcfNotificationSubscriptionService));

            serviceHost.Extensions.Add(
                new NotificationSubscriptionReceiverExtension(
                    OnNotificationSubscription,
                    OnNotificationUnsubscription));

            serviceHost.Open();
        } 

        /// <summary>
        /// Stops hosting the <see cref="WcfNotificationSubscriptionService"/>.
        /// </summary>
        public void Stop()
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
                serviceHost = null;
            }
        }

        /// <summary>
        /// Raises the NotificationSubscription event.
        /// </summary>
        /// <param name="returnAddress">The return address of the subscriber.</param>
        /// <param name="profile">The subscription profile.</param>
        protected virtual void OnNotificationSubscription(Uri returnAddress, SubscriptionProfile profile)
        {
            this.NotificationSubscription.Fire(this, new NotificationSubscriptionEventArgs(returnAddress, profile));
        }

        /// <summary>
        /// Raises the NotificationUnsubscription event.
        /// </summary>
        /// <param name="returnAddress">The return address.</param>
        protected virtual void OnNotificationUnsubscription(Uri returnAddress)
        {
            this.NotificationUnsubscription.Fire(this, new NotificationUnsubscriptionEventArgs(returnAddress));
        }
    }
}
